/**
 * 一维数组递归树形数组结构
 * @param params 一维数组
 * @param index  默认下表
 * @returns 
 */

export function listToTree(params: Array<any>, index: any = 0): Array<Object> {
    let result: Array<Object> = []
    if (params.length === 0 || params.length === 1) {
        return params
    }
    if (params[index].pId !== 0) {
        params.forEach(item => {
            if (item.id === params[index].pId) {
                if (!item.children) {
                    item.children = [];
                    console.log(item)
                    item.children.push(params[index])
                } else {
                    item.children.push(params[index])
                }
            }
        })
    }
    if (index === params.length - 1) {
        params.forEach(item => {
            if (item.pId === 0) {
                result.push(item)
            }
        })
        return result
    } else {
        index++
        return listToTree(params, index)
    }
}
/**
 * 获取该记录的父级id
 */
interface Ids2stringTypr {
    id: Number,
    pId: Number
}
export const ids2string = (array: Array<Ids2stringTypr>, item: any): Array<Number> => {
    const result: Array<Number> = []
    dg(array, item)
    return result
    function dg(array: Array<Ids2stringTypr>, item: any) {
        result.push(item.id)
        if (item.pId === 0) {
            return result
        } else {
            array.forEach(object => {
                if (object.id === item.pId) {
                    return dg(array, object)
                }
            })
        }
    }
}
/**
 * 获取该记录的子级id
 */
export const ids2stringChildren = (item: any): Array<Number> => {
    const result: Array<Number> = []
    dg(item)
    return result
    function dg(item: any) {
        result.push(item.id)
        if (!item.children) {
            return result
        } else {
            item.children.forEach((item: any) => {
                dg(item)
            })
        }
    }
}